import { isNull } from 'undraw-ui'
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/index.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: '/demo',
    name: 'demo',
    component: () => import('@/views/demo/index.vue'),
    meta: {
      title: '演示'
    }
  },
  {
    path: '/crud',
    name: 'crud',
    component: () => import('@/views/crud/index.vue'),
    meta: {
      title: '表格'
    }
  },
  {
    path: '/table',
    name: 'table',
    component: () => import('@/views/table/index.vue'),
    meta: {
      title: '表格'
    }
  },
  {
    path: '/form',
    name: 'form',
    component: () => import('@/views/form/index.vue'),
    meta: {
      title: '表单'
    }
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('@/views/test/index.vue'),
    meta: {
      title: '测试'
    }
  },
  {
    path: '/context-menu',
    name: 'context-menu',
    component: () => import('@/views/context-menu/index.vue'),
    meta: {
      title: '背景菜单'
    }
  },
  {
    path: '/undraw-ui',
    name: 'undraw-ui',
    component: () => import('@/views/undraw-ui/index.vue'),
    meta: {
      title: 'undraw-ui'
    }
  },
  {
    path: '/copy',
    name: 'copy',
    component: () => import('@/views/copy/index.vue'),
    meta: {
      title: 'copy'
    }
  },
  {
    path: '/wang-editor',
    name: 'wang-editor',
    component: () => import('@/views/wang-editor/index.vue'),
    meta: {
      title: 'wang-editor'
    }
  },
  {
    path: '/issues',
    name: 'issues',
    component: () => import('@/views/issues/index.vue'),
    meta: {
      title: 'issues'
    }
  },
  {
    path: '/upload',
    name: 'upload',
    component: () => import('@/views/upload/index.vue'),
    meta: {
      title: 'upload'
    }
  },
  {
    path: '/file',
    name: 'file',
    component: () => import('@/views/file/index.vue'),
    meta: {
      title: 'file'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: routes
})

router.beforeEach(async (to, form, next) => {
  let title = to.meta.title as string
  document.title = title ? title : isNull(document.querySelector('title')?.text, 'loading...')
  next()
})

export default router
